﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using MP.LayerHelper.MP_OBS.BLL;
using MP.LayerHelper.MP_OBS.DAL.EntityClasses;
using MP.LayerHelper.MP_OBS.DAL.HelperClasses;

using MP.Core.Library;
using MP.Core.Config;
public partial class Admin_Hurman_Controls_Position : MP.Core.Authentication.Control.AuthenBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadDataPosition();
            loadDataUser();
            loadCombo();
        }
    }
    public string Branch
    {
        get { return !string.IsNullOrEmpty(Request["branch"]) ? Request["branch"] : ""; }
    }

    public string Depart
    {
        get { return !string.IsNullOrEmpty(Request["depart"]) ? Request["depart"] : ""; }
    }

    public string Job
    {
        get { return !string.IsNullOrEmpty(Request["job"]) ? Request["job"] : ""; }
    }

    public string Group
    {
        get { return !string.IsNullOrEmpty(Request["g"]) ? Request["g"] : ""; }
    }

    public string users
    {
        get { return !string.IsNullOrEmpty(Request["u"]) ? Request["u"] : ""; }
    }
    
    void loadDataPosition()
    {
        ddlBranch1.Text = Branch;
        ddlDepart1.Text = Depart;
        DataSet dt = HrPositionManager.CreateInstant().GetPosition(Branch, Depart, Job, Group, users);
        if (dt.Tables[0].Rows.Count > 0)
        {
            smartPager.Visible = true;
            PagedDataSource pagedata = new PagedDataSource();
            string url = "/admin/Hurman/Position.aspx";
            string paramURL = "?branch={0}&depart={1}&job={2}&g={3}&u={4}";
            paramURL = string.Format(paramURL, Server.UrlDecode(Branch)
                                             , Server.UrlDecode(Depart)
                                             , Server.UrlEncode(Job)
                                             , Server.UrlEncode(Group)
                                             , Server.UrlEncode(users));
            pagedata = Func.GetPaging(dt.Tables[0], Request["page"], 1000);
            this.smartPager.RowCount = pagedata.DataSourceCount;
            this.smartPager.CurrentPage = pagedata.CurrentPageIndex + 1;
            this.smartPager.PageSize = pagedata.PageSize;
            this.smartPager.Visible = this.smartPager.RowCount > this.smartPager.PageSize;
            this.smartPager.UrlFormatString = url + paramURL + "&page={0}";
            repPosition.DataSource = pagedata;
            repPosition.DataBind();
        }
    }

    void loadDataUser()
    {
        DataSet dst = HrPositionManager.CreateInstant().GetNewUser();
        repUsers.DataSource = dst;
        repUsers.DataBind();
    }

    void loadCombo()
    {
        ddlBranch1.DataSource = HrBranchManager.CreateInstant().GetAllBranch();
        ddlBranch1.DataTextField = "BranhName";
        ddlBranch1.DataValueField = "BranhCode";
        ddlBranch1.DataBind();

        ddlBranch.DataSource = HrBranchManager.CreateInstant().GetAllBranch();
        ddlBranch.DataTextField = "BranhName";
        ddlBranch.DataValueField = "Id";
        ddlBranch.DataBind();

        ddlDepart1.DataSource = MaDepartmentManager.CreateInstant().GetAllDepart();
        ddlDepart1.DataTextField = "DepartName";
        ddlDepart1.DataValueField = "DepartCode";
        ddlDepart1.DataBind();

        ddlJob1.DataSource = HrJobTitleManager.CreateInstant().GetAllJob();
        ddlJob1.DataTextField = "JobName";
        ddlJob1.DataValueField = "JobCode";
        ddlJob1.DataBind();

        ddlGroup1.DataSource = MaAgentGroupManager.CreateInstant().GetAll();
        ddlGroup1.DataTextField = "AgentGroupName";
        ddlGroup1.DataValueField = "AgentGroupCode";
        ddlGroup1.DataBind();
    }

    public string getImageSub(int numSub)
    {
        if (numSub > 0)
        {
            return "<img src='/images/button/arrow.gif'/>";
        }
        return "";
    }
    protected void btSearch_Click(object sender, EventArgs e)
    {
        if (Page.IsValid && IsShow())
        {
            string url = "/admin/Hurman/Position.aspx";
            string paramURL = "?branch={0}&depart={1}&job={2}&g={3}&u={4}&page=1";
            paramURL = string.Format(paramURL, Server.UrlEncode(ddlBranch1.SelectedValue)
                                             , Server.UrlEncode(ddlDepart1.SelectedValue)
                                             , Server.UrlEncode(ddlJob1.SelectedValue)
                                             , Server.UrlEncode(ddlGroup1.SelectedValue)
                                             , Server.UrlEncode(txtuser.Text.Trim()));
            Response.Redirect(url + paramURL);
        }
        else if (!IsShow())
        {
            RedirectLogin();
        }
    }
    protected void repPosition_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "del":
                string username=e.CommandArgument.ToString();
                HrPositionEntity hrObj = HrPositionManager.CreateInstant().GetByUserID(username);
                if(hrObj!=null)
                {
                    hrObj.IsDeleted = true;
                    hrObj.UpdatedBy = Util.CurrentUserName;
                    hrObj.UpdatedDate = DateTime.Now;
                    HrPositionManager.CreateInstant().Update(hrObj);
                }
                break;
        }
        Response.Redirect(Request.RawUrl);
    }
}
